package org.thunms.framework.freemarker;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Locale;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thunms.framework.utils.UtilSystemProperties;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
 * 模板解析工具类
 * @author Administrator
 *
 */
public class FreemarkerUtil {
	private static final Logger logger = LoggerFactory.getLogger(FreemarkerUtil.class);
		//getTemplate("01.ftl")
		public static Template getTemplate(String name) {
			
				try {
					//通过Freemaker的Configuration读取相应的ftl
					Configuration cfg = new Configuration();
					//设定去哪里读取相应的ftl模板文件
					StringBuffer p=new StringBuffer(UtilSystemProperties.getThunmsWebappRoot()).append("/WEB-INF/t");
					File templateDirectory=new File (p.toString());
					cfg.setDirectoryForTemplateLoading(templateDirectory);
					//在模板文件目录中找到名称为name的文件
					cfg.setDefaultEncoding("UTF-8");
					cfg.setLocale(Locale.CHINESE);
					return cfg.getTemplate(name);
				} catch (IOException e) {
					logger.error("解析模板路径出现读取错误",e);
				}
				return null;
			}
		
		
		
		
		public static String print(String name,Map<String,Object> params) {
			String rt=null;
			try {
				
				//通过Template可以将模板文件输出到相应的流
				Template temp = getTemplate(name);
				if(temp!=null){
					StringWriter out = new StringWriter();
					//ByteArrayOutputStream bos=new ByteArrayOutputStream();
					//Writer out = new BufferedWriter(new OutputStreamWriter(bos,"UTF-8"));
					temp.process(params, out);
					rt=out.getBuffer().toString(); 
					out.flush();
					out.close();
				}
				logger.debug("通过模板解析后的内容:=>"+rt);
			} catch (TemplateException e) {
				logger.error("解析模板路径出现解析错误",e);
			} catch (IOException e) {
				logger.error("解析模板路径出现读取错误",e);
			}
			return rt;
		}
		
		public static String print(String name,Map<String,Object> params,Configuration cfg) {
			String rt=null;
			try {
				
				//通过Template可以将模板文件输出到相应的流
				Template temp = cfg.getTemplate(name);
				if(temp!=null){
					StringWriter out = new StringWriter();
					//ByteArrayOutputStream bos=new ByteArrayOutputStream();
					//Writer out = new BufferedWriter(new OutputStreamWriter(bos,"UTF-8"));
					temp.process(params, out);
					rt=out.getBuffer().toString(); 
					out.flush();
					out.close();
				}
				logger.debug("通过模板解析后的内容:=>"+rt);
			} catch (TemplateException e) {
				logger.error("解析模板路径出现解析错误",e);
			} catch (IOException e) {
				logger.error("解析模板路径出现读取错误",e);
			}
			return rt;
		}
		
		/**
		 * 使用字符串做模板内容进行解析
		 * @param params
		 * @param template
		 * @return
		 */
		public static String print(Map<String,Object> params,String template){
			String rt=null;
			Configuration cfg = new Configuration();  
	        cfg.setTemplateLoader(new StringTemplateLoader(template));  
	        cfg.setDefaultEncoding("UTF-8");  
	        try {
				Template t = cfg.getTemplate("");
				StringWriter out = new StringWriter();
				//ByteArrayOutputStream bos=new ByteArrayOutputStream();
				//Writer out = new BufferedWriter(new OutputStreamWriter(bos,"UTF-8"));
				t.process(params, out);
				rt=out.getBuffer().toString(); 
				out.flush();
				out.close();
				logger.debug("通过模板解析后的内容:=>"+rt);
			} catch (TemplateException e) {
				logger.error("解析模板路径出现解析错误:模板参数===>>"+params);
				logger.error("解析模板路径出现解析错误:模板内容为===>>"+template,e);
			} catch (IOException e) {
				logger.error("解析模板路径出现读取错误:模板参数===>>"+params);
				logger.error("解析模板路径出现读取错误:模板内容为===>>"+template,e);
			} 
			return rt;
			
		}
		
		
		
}
